package org.lemon.cla.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.lemon.cla.pojo.Feedback;
import org.lemon.cla.vo.FeedbackVo; // 导入 FeedbackVo
import org.lemon.cla.dto.request.FeedbackSearchRequest;

@Mapper
public interface FeedbackMapper extends BaseMapper<Feedback> {

    // 联查用户表的公共字段片段
    String JOIN_USER_SELECT_FIELDS =
            "u.username AS username, " + // 对应 FeedbackVo 的 username
                    "u.avatar_url AS avatarUrl, " + // 对应 FeedbackVo 的 avatarUrl
                    "u.real_name AS realName, " + // 对应 FeedbackVo 的 realName
                    "u.phone AS phone, " + // 对应 FeedbackVo 的 phone
                    "u.email AS email, " + // 对应 FeedbackVo 的 email
                    "u.role AS userRole, " + // 对应 FeedbackVo 的 userRole
                    "u.is_deleted AS userIsDeleted "; // 对应 FeedbackVo 的 userIsDeleted

    /**
     * 根据反馈ID获取反馈详情，包含用户信息。
     * @param feedbackId 反馈ID
     * @return 反馈视图对象
     */
    @Select("SELECT f.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM feedback f " +
            "LEFT JOIN user u ON f.user_id = u.user_id " +
            "WHERE f.feedback_id = #{feedbackId}")
    FeedbackVo selectFeedbackVoById(@Param("feedbackId") Long feedbackId);

    /**
     * 分页查询所有反馈，包含用户信息。
     * @param page 分页对象
     * @return 分页反馈视图对象列表
     */
    @Select("SELECT f.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM feedback f " +
            "LEFT JOIN user u ON f.user_id = u.user_id " +
            "ORDER BY f.create_time DESC")
    IPage<FeedbackVo> selectFeedbackVoPage(IPage<FeedbackVo> page);

    /**
     * 根据多个条件模糊查询反馈，包含用户信息。
     * @param page 分页对象
     * @param req 查询请求DTO
     * @return 分页反馈视图对象列表
     */
    @Select("<script>" +
            "SELECT f.*, " +
            JOIN_USER_SELECT_FIELDS +
            "FROM feedback f " +
            "LEFT JOIN user u ON f.user_id = u.user_id " +
            "<where>" +
            "  <if test='req.userId != null'>" +
            "    AND f.user_id = #{req.userId}" +
            "  </if>" +
            "  <if test='req.feedbackType != null'>" +
            "    AND f.feedback_type = #{req.feedbackType}" +
            "  </if>" +
            "  <if test='req.content != null and req.content != \"\"'>" +
            "    AND f.content LIKE CONCAT('%', #{req.content}, '%')" +
            "  </if>" +
            "  <if test='req.contact != null and req.contact != \"\"'>" +
            "    AND f.contact LIKE CONCAT('%', #{req.contact}, '%')" +
            "  </if>" +
            "  <if test='req.createTimeStart != null'>" +
            "    AND f.create_time &gt;= #{req.createTimeStart}" +
            "  </if>" +
            "  <if test='req.createTimeEnd != null'>" +
            "    AND f.create_time &lt;= #{req.createTimeEnd}" +
            "  </if>" +
            "  <if test='req.status != null'>" +
            "    AND f.status = #{req.status}" +
            "  </if>" +
            "  <if test='req.reply != null and req.reply != \"\"'>" +
            "    AND f.reply LIKE CONCAT('%', #{req.reply}, '%')" +
            "  </if>" +
            "  <if test='req.replyTimeStart != null'>" +
            "    AND f.reply_time &gt;= #{req.replyTimeStart}" +
            "  </if>" +
            "  <if test='req.replyTimeEnd != null'>" +
            "    AND f.reply_time &lt;= #{req.replyTimeEnd}" +
            "  </if>" +
            "</where>" +
            "ORDER BY f.create_time DESC" +
            "</script>")
    IPage<FeedbackVo> selectFeedbacksVoByMultiConditions(
            IPage<FeedbackVo> page,
            @Param("req") FeedbackSearchRequest request
    );
}
